{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from autograd import Tensor, Parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax0 = plt.subplots()\n",
    "fig, ax1 = plt.subplots()\n",
    "x0 = np.linspace(-10, 10, 100)\n",
    "x1 = np.linspace(-4, 4, 100)\n",
    "sigmoid = 1/(1+np.exp(-x0))\n",
    "tanh = 2/(1+np.exp(-2*x0))-1\n",
    "relu = np.maximum(0,x1)\n",
    "leaky_relu = np.maximum(0,x1) + 0.1*np.minimum(x1,0)\n",
    "elu = np.maximum(0,x1) + np.minimum(0,0.5*(np.exp(x1)-1))\n",
    "#plt.plot(x, y, color='blue')\n",
    "ax0.plot(x0, sigmoid, color='b')\n",
    "# ax.title('activate')\n",
    "ax0.plot(x0, tanh, \"r\")\n",
    "ax1.plot(x1, relu, \"k\")\n",
    "ax1.plot(x1, leaky_relu, \"r\")\n",
    "ax1.plot(x1, elu, \"b\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x2_v = np.random.rand(10)\n",
    "x2_i = list(np.arange(1,11))\n",
    "softmax = np.around(np.exp(-x2_v)/np.exp(-x2_v).sum(),6)\n",
    "# plt.hist(softmax, bins = x2_i) \n",
    "plt.bar(x2_i, softmax)\n",
    "plt.xticks(x2_i)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([\"Runoob-1\", \"Runoob-2\", \"Runoob-3\", \"C-RUNOOB\"])\n",
    "y = np.array([12, 22, 6, 18])\n",
    "\n",
    "plt.barh(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.randint(0,10,10)\n",
    "print(list(np.array(np.arange(1,11))))\n",
    "print(-x)\n",
    "np.around(np.exp(-x)/np.exp(-x).sum(),6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = Tensor(np.ones((2,2)),requires_grad=True)\n",
    "p = Parameter(x)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = Tensor(np.ones((2,2)),requires_grad=True)\n",
    "x2 = Tensor(np.ones((2,1)),requires_grad=True)\n",
    "x3 = Tensor(np.ones((2,1)),requires_grad=True)\n",
    "print(x1)\n",
    "print(x2)\n",
    "#print(x3)\n",
    "o1 = x1 * x2\n",
    "print(o1)\n",
    "z1 = o1.sum()\n",
    "print(o1)\n",
    "print(z1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z1.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o1.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(x1)\n",
    "print(x1.grad)\n",
    "print(x2)\n",
    "print(x2.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m1 = Tensor(np.ones((2,2)),requires_grad=True)\n",
    "print(m1)\n",
    "out = m1.sum()\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m1.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = np.ones((2,1))*2\n",
    "x2 = np.ones((1,2))\n",
    "print(x1,x2)\n",
    "x3 = x1 @ x2\n",
    "x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t1 = Tensor(np.ones((2,1))*2,requires_grad=True)\n",
    "t2 = Tensor(np.ones((1,2)),requires_grad=True)\n",
    "print(t1,t2)\n",
    "t3 = t1 + t2\n",
    "print(t3)\n",
    "t4 = t3.sum()\n",
    "print(t4)\n",
    "t4.backward()\n",
    "print(t1.grad)\n",
    "print(t2.grad)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for o1,o2 in zip((t1,t2),(t1,t2)):\n",
    "    print(o1)\n",
    "    print(o2)\n",
    "    print(\"--\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t1 = Tensor(np.ones((2,1))*2,requires_grad=True)\n",
    "t2 = Tensor(np.ones((1,2)),requires_grad=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t3 = t2 @ t1\n",
    "t3.backward()\n",
    "print(t1.grad)\n",
    "print(t2.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from autograd import Tensor\n",
    "import autograd.nn as nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor([[ 0.78932739  0.29889405  0.54565407  0.17720068 -0.40610353 -0.20014472]\n",
      " [ 0.64138517  0.23320539  0.84816816  0.1428923  -0.31937729 -0.7679765 ]\n",
      " [-0.40095189 -0.93836779 -0.72295669  0.37179099 -0.47916655 -0.86996704]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Tensor([[0.71812582 0.67690197 0.35225365 0.18665772 0.29407904 0.61914713]\n",
       " [0.57860866 0.44866757 0.46954841 0.00460413 0.95565653 0.5901954 ]], requires_grad=True)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = Tensor(np.array([[1., 2., 3.],[1., 2., 3.]]))\n",
    "t2 = Tensor(np.zeros([3,6]))\n",
    "t3 = Tensor(np.random.uniform(size=[2,6]))\n",
    "t4 = Tensor.uniform(-1, 1, size=[3,6])\n",
    "print(t4)\n",
    "fc = nn.Linear(6, 2, bias=False)\n",
    "fc.weight\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor([[0.7510973  0.3416226 ]\n",
      " [0.3744821  0.11618543]\n",
      " [0.         0.        ]], requires_grad=True, grad_fn=<class 'autograd.functions.Relu'>)\n"
     ]
    }
   ],
   "source": [
    "relu = nn.ReLU()\n",
    "out = fc(t4)\n",
    "out = relu(out)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Tensor.uniform(size=[3])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "72afde127a681a9c4291b943fce7674ba5dee3f782a7f671a875411cb5f75bf9"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
